package com.tanhua.server.serivce;

import com.tanhua.commons.template.OssTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Sound;
import com.tanhua.domain.vo.SoundVo;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.PeachblossomApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class PeachblossomService {

    @Autowired
    private OssTemplate ossTemplate;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Reference
    private PeachblossomApi peachblossomApi;
    @Reference
    private UserInfoApi userInfoApi;

    private String REMAINING_TIME = "REMAINING_TIME_";

    /**
     *接口名称：桃花传音-发送语音
     */
    public ResponseEntity<Object> sendSound(MultipartFile soundFile) throws IOException {
        //获取发送语音者id（当前用户）
        Long userId = UserHolder.getUserId();

        String name = soundFile.getOriginalFilename();
        //将音频上传到oss
        String voiceUrl = ossTemplate.upload(name, soundFile.getInputStream());

        //调用api保存信息到mongo
        peachblossomApi.saveVocie(voiceUrl,userId);

        return ResponseEntity.ok(null);
    }

    /**
     * 接口名称：桃花传音-接收语音
     * @return
     */
    public ResponseEntity<Object> getSound() {
        Long userId = UserHolder.getUserId();
        //查找所有语音
        List<Sound> soundList = peachblossomApi.findAllSound();

        if (redisTemplate.opsForValue().get(REMAINING_TIME + userId) == null) {
            redisTemplate.opsForValue().set(REMAINING_TIME + userId,10+"");
        }
        String time = redisTemplate.opsForValue().get(REMAINING_TIME + userId) ;
        redisTemplate.opsForValue().set(REMAINING_TIME + userId,(Integer.parseInt(time)-1)+"");
        time = (Integer.parseInt(time)-1)+"";
        //创建封装返回的集合数据
        List<SoundVo> soundVoList = new ArrayList<>();
        if (soundList != null && soundList.size()>0) {
            for (Sound sound : soundList) {
                SoundVo soundVo = new SoundVo();
                BeanUtils.copyProperties(sound,soundVo);

                //根据id查找userinfo
                UserInfo userInfo = userInfoApi.findById(sound.getUserId());

                if (userInfo != null) {
                    BeanUtils.copyProperties(userInfo, soundVo);
                }
                //封装其他数据
                soundVo.setId(sound.getUserId().intValue());

                soundVo.setRemainingTimes(Integer.parseInt(time));
                soundVoList.add(soundVo);
            }
        }
        int index = new Random().nextInt(soundVoList.size());
        SoundVo soundVo = soundVoList.remove(index);

        return ResponseEntity.ok(soundVo);
    }
}
